/**
 * ===========================================================================
 * 版权所有 湖南免了么网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.mianle.me
 * ===========================================================================
 * 21:15 2018/6/9
 */
package com.mianle.userapi.controller;

import com.mianle.userapi.exception.CustomizeException;
import com.mianle.userapi.service.RedisGeo;
import com.mianle.userapi.service.RedisService;
import com.mianle.userapi.util.*;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yiyuan
 * @Description:
 * @Date: Created in 21:15 2018/6/9
 *
 */
@RestController
@RequestMapping("geo")
@Slf4j
public class RdisGeoController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisGeo redisGeo;

    public JsonResponseData saveRedisKey() {
        try {
            int i = redisGeo.saveRedisKey();
            return null;
        } catch (CustomizeException e) {
            return JsonResponseEnum.OPERATION_FAILED.result();
        } catch (Exception e) {
            return JsonResponseEnum.SERVER_INNER_ERROR.result();
        }
    }

    /*
        纬度---lat----x轴
        经度---lon---y轴
    */

    @RequestMapping("geoadd")
    public boolean geoadd() {
        return redisGeo.geoadd("张三", 116.373472, 39.972528);
    }

    @RequestMapping("cacheGeo")
    public Boolean get1() {
//        //经度 Y
//        double lngd = Double.valueOf(lng);
//        //纬度 X
//        double latd = Double.valueOf(lat);
//        String hash = LbsUtil.geoHashEncode(lngd, latd, 12);

        List<GeoLocation<String>> ite = Lists.newArrayList();

        GeoLocation<String> geo1 = new GeoLocation<String>("黄花机场sd", new Point(28.1887,113.226255));
        ite.add(geo1);
        GeoLocation<String> geo2 = new GeoLocation<String>("长沙生态动物园", new Point(28.033517,113.002907));
        ite.add(geo2);
        GeoLocation<String> geo3 = new GeoLocation<String>("湖南省政府", new Point(28.116367,112.98248));
        ite.add(geo3);
        GeoLocation<String> geo4 = new GeoLocation<String>("长沙市中心医院", new Point(28.142557,112.997758));
        ite.add(geo4);
        GeoLocation<String> geo5 = new GeoLocation<String>("中电软件园总部", new Point(28.230011,112.882562));
        ite.add(geo5);
        GeoLocation<String> geo6 = new GeoLocation<String>("园区公租房", new Point(28.23038,112.880137));
        ite.add(geo6);
        GeoLocation<String> geo7 = new GeoLocation<String>("德元大包", new Point(28.229529,112.878319));
        ite.add(geo7);
        GeoLocation<String> geo8 = new GeoLocation<String>("园区餐厅", new Point(28.229269,112.876098));
        ite.add(geo8);
        GeoLocation<String> geo9 = new GeoLocation<String>("安智教育", new Point(28.229723,112.874784 ));
        ite.add(geo9);
        GeoLocation<String> geo10 = new GeoLocation<String>("吃喝玩乐别墅轰趴", new Point(28.197096,112.902775 ));
        ite.add(geo10);
        GeoLocation<String> geo11 = new GeoLocation<String>("纤云美丽生活馆", new Point(28.195186,112.89975  ));
        ite.add(geo11);
        GeoLocation<String> geo12 = new GeoLocation<String>("食流疯概念餐厅", new Point(28.142268,112.937365));
        ite.add(geo12);
        GeoLocation<String> geo13 = new GeoLocation<String>("凯德广场", new Point(28.154072,112.995358 ));
        ite.add(geo13);
        GeoLocation<String> geo14 = new GeoLocation<String>("瑞祥客房", new Point(28.150628,112.99538 ));
        ite.add(geo14);
        GeoLocation<String> geo15 = new GeoLocation<String>("不怕辣烧鸡公", new Point(28.148689,112.999789 ));
        ite.add(geo15);
        GeoLocation<String> geo16 = new GeoLocation<String>("蒸湘坊", new Point(28.229385,112.878771 ));
        ite.add(geo16);
        GeoLocation<String> geo17 = new GeoLocation<String>("健怡缘中医经咯养生会所", new Point(28.225234,112.882694 ));
        ite.add(geo17);
        GeoLocation<String> geo18 = new GeoLocation<String>("沈婷食品店", new Point(28.225744,112.879808 ));
        ite.add(geo18);

        for (GeoLocation<String> location : ite) {
            String k = LbsUtil.geoHashEncode(location.getPoint().getY(), location.getPoint().getX(), 3);
            redisGeo.cacheGeo(k, location.getPoint().getX(), location.getPoint().getY(), location.getName(), 1000*60*10);
        }

        return true;
        //return redisGeo.cacheGeo(hash, latd, lngd, member, 1000*60*10);
    }


    @GetMapping("nearbyStore")
    public List<? extends GeoResult<RedisGeoCommands.GeoLocation<String>>> nearbyStore() {
        System.out.println("jrebel------------测试"+"ces");
        System.out.println("luyujiao");
        double lng = 112.881042;
        double lat = 28.228963;
        String hash = LbsUtil.geoHashEncode(lng, lat, 3);
        SearchGeo param = new SearchGeo(hash, lng, lat, 10D, RedisGeoCommands.DistanceUnit.KILOMETERS);
        return redisGeo.radiusGeo(param);
    }

    public static void main(String[] args) {
        double lng = 112.881042;
        double lat = 28.228963;
        String hash = LbsUtil.geoHashEncode(lng, lat, 3);
        System.out.println("---------------------");

        Number num = new Integer(1);
        //type mismatch
       // ArrayList<Number> list = new ArrayList<>();
        List<? extends Number> list  =  new ArrayList<Number>();

    }




}
